package net.main.dartcounter;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

public class PlayerHandler {
	private DartCounter instance;
	
	private String name;
	private double avgScorePerArrow;
	private int thrownArrows;
	
	private int[] singles;
	private int[] doubles;
	private int[] triples;
	private int bull;
	private int bulls_eye;
	
	private int wins;
	private int losses;
	
	File filePlayer;
	
	public PlayerHandler(DartCounter instance) {
		this("No-Name", instance);
	}
	
	public PlayerHandler(String name, DartCounter instance) {
		if(name.equals("no-name") || name.equalsIgnoreCase("")) {
			for(;;) {
				if(valid_Name(name)) {
					break;
				} else {
					name = JOptionPane.showInputDialog("Enter a valid name!\n" +
							"You may only use the characters a-z, A-Z, - and _.");
				}
			}
			
			
			
		}
	}
	
	public void init_Stats(String name) {
		filePlayer = new File(instance.getDir() + "/" + name + ".dacs");
		if(check_New_Player(name)) {
			try {
				filePlayer.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(instance, "An error occured hwile creating a new file for player <red>" + name + "</red>!");
			}
		}
	}
	
	public boolean valid_Name(String name) {
		return name.matches("^(a-zA-Z_\\-){+}$");
	}
	
	public boolean check_New_Player(String name) {
		return !filePlayer.exists();
	}
}
